package com.imy.lua.lib.device;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.PowerManager;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;

import com.immomo.mls.LuaViewManager;
import com.immomo.mls.annotation.LuaBridge;
import com.immomo.mls.annotation.LuaClass;
import com.immomo.mls.util.FileUtil;
import com.immomo.mls.wrapper.callback.IVoidCallback;
import com.imy.common.root.RootTools;
import com.imy.common.util.ALog;
import com.imy.common.util.DeviceUtils;
import com.imy.common.util.FileUtils;
import com.imy.common.util.UiUtil;
import com.imy.lua.LuaCore;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;

/**
 * Created by MLN Template 注册方法: Register.newUDHolderWithLuaClass(UDDevice.LUA_CLASS_NAME, UDDevice.class, true) 若想在Java层实现参数自动转换，则增加注册:
 *
 * @see com.immomo.mls.MLSBuilder#registerCovert
 * @see com.immomo.mls.MLSBuilder.CHolder new MLSBuilder.CHolder(UDDevice.class)
 */
@LuaClass(gcByLua = false)
public class UDDevice {
  /**
   * Lua类型，Lua可由此创建对象: local obj = Device()
   */
  public static final String LUA_CLASS_NAME = "Device";
  private AudioManager mAudioManager;
  private PowerManager mPowerManager;
  private PowerManager.WakeLock wakeLock;
  private static final String WAKE_LOCK_TAG = "UDDevice:ScreenKeeper";
  private static final long MAX_TEST_DURATION = 24 * 60 * 60 * 1000;
  /**
   * Lua构造函数
   *
   * @param g    虚拟机
   * @param init 构造方法中传入的参数
   */
  public UDDevice(@NonNull Globals g, @NonNull LuaValue[] init) {
    mAudioManager = (AudioManager) LuaCore.get().getContext().getSystemService(Context.AUDIO_SERVICE);
    mPowerManager = (PowerManager) LuaCore.get().getContext().getSystemService(Context.POWER_SERVICE);

  }

  @LuaBridge
  public int width() {
    return UiUtil.getDeviceWidth(LuaCore.get().getContext());
  }

  @LuaBridge
  public int getHeight() {
    return UiUtil.getDeviceHeight(LuaCore.get().getContext());
  }

  @LuaBridge
  public int sdkInit() {
    return Build.VERSION.SDK_INT;
  }

  @LuaBridge
  public String brand() {
    return Build.BRAND;
  }

  @LuaBridge
  public String device() {
    return Build.DEVICE;
  }

  @LuaBridge
  public String model() {
    return Build.MODEL;
  }

  @LuaBridge
  public String product() {
    return Build.PRODUCT;
  }

  @LuaBridge
  public String bootloader() {
    return Build.BOOTLOADER;
  }

  @LuaBridge
  public String serial() {
    return Build.SERIAL;
  }

  @LuaBridge
  public String getIMEI() {
    return DeviceUtils.getIMEI(LuaCore.get().getContext());
  }

  @LuaBridge
  public String getMacAddress() {
    return DeviceUtils.getMacAddress(LuaCore.get().getContext());
  }
  @LuaBridge
  public int getOrientation(){
    return LuaCore.get().getAccessibilityServiceListener().getOrientation();
  }

  /**
   * 媒体音量
   *
   * @return
   */
  @LuaBridge
  public int getMaxMusicVolume() {
    return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  }

  /**
   * 媒体音量
   *
   * @return
   */
  @LuaBridge
  public int getCurrentMusicVolume() {
    return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
  }

  @LuaBridge
  public void setMusicVolume(float percent) {
    int streamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int v = (int) (streamMaxVolume * percent);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,v,0);
  }

  /**
   * 通话音量
   *
   * @return
   */
  @LuaBridge
  public int getMaxVoiceVolume() {
    return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
  }

  /**
   * 通话音量
   *
   * @return
   */
  @LuaBridge
  public int getCurrentVoiceVolume() {
    return mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
  }

  @LuaBridge
  public void setVoiceVolume(float percent) {
    int streamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int v = (int) (streamMaxVolume * percent);
    mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,v,0);
  }

  @LuaBridge
  public boolean isRoot(){
    return RootTools.isRootAvailable();
  }
  @LuaBridge
  public void wakeUp(){
    releaseWakeLock();
    wakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
    try {
      wakeLock.acquire(MAX_TEST_DURATION);
    } catch (Exception e) {
      ALog.e( "Security Exception" + e);
    }
  }
  //<editor-fold desc="Bridge API">

  /**
   * 获取上下文，一般情况，此上下文为Activity
   *
   * @param globals 虚拟机，可通过构造函数存储
   */
  protected Context getContext(@NonNull Globals globals) {
    LuaViewManager m = (LuaViewManager) globals.getJavaUserdata();
    return m != null ? m.context : null;
  }
  private void releaseWakeLock() {
    if (wakeLock == null) {
      return;
    }

    try {
      wakeLock.release();
    } catch (Exception e) {/* ignore */}
    wakeLock = null;
  }
}
